
import AbsWindow from "../../framework/AbsWindow";
import { WindowName } from "../../framework/reflect/WindowName";
import MenuWindow from "./MenuWindow";
import Context from "../../framework/Context";
import GameWindow from "./GameWindow";
import LocalStorage from "../../framework/utils/LocalStorage";
import { WxEvent } from "../../framework/wx/WxEvent";
import LoadResData from "../../framework/LoadResData";
import Wx from "../../framework/wx/Wx";
import { SubContextView } from "../comp/SubContextView";
const { ccclass, property } = cc._decorator;
@ccclass
@WindowName("ForgeWindow")
export default class ForgeWindow extends AbsWindow {

    @property(cc.Node)
    private returnBtn: cc.Node = null;
    @property(cc.Node)
    private startBtn: cc.Node = null;
    @property(cc.Label)
    private weaponLv: cc.Label = null;
    @property(cc.Label)
    private curLv: cc.Label = null;
    @property(cc.Label)
    private lastLv: cc.Label = null;
    @property(cc.Label)
    private gold: cc.Label = null;
    @property(cc.ProgressBar)
    private lvProgressBar: cc.ProgressBar = null;
    public onShow() {
        this.setClickListener(this.returnBtn, this.onReturnBtn);
        //this.setClickListener(this.startBtn, this.onStartBtn);
        this.showUI();
    }

    public listen(): string[] {
        return [WxEvent.ON_FORGINGSUCCESS]
    }

    public handleMessage(message: string, args: any[]): boolean {
        var self = this;
        switch (message) {
            case WxEvent.ON_FORGINGSUCCESS: {
                ForgeWindow.upDataWeaponLv(self, args[0]);
                break;
            }
        }
        return false
    }
    private onReturnBtn() {
        this.context.closeWindow(ForgeWindow);
    }
    private onStartBtn() {
        //this.context.closeWindow(ForgeWindow);
        var playerObj = LocalStorage.getObj("Player");
        var list = [];
        var lv = playerObj["oneselfWeapon"].lv;
        var weaponInfo = {
            id: 1001
        }
        list.push(weaponInfo);
        list.push(lv);
        GameWindow.sendData(list);
    }
    public showUI() {
        var playerObj = LocalStorage.getObj("Player");
        var totalGold = playerObj["gold"];
        var needGold = 0;
        var lv = playerObj["oneselfWeapon"].lv;
        var curLvState = playerObj["oneselfWeapon"].curLvState;
        var lvUpData = LoadResData.lvupData;
        for (let i = 0; i < lvUpData.length; i++) {
            if (lv == lvUpData[i]["lv"]) {
                needGold = lvUpData[i]["price"]
            }
        }
        this.curLv.string = 'Lv' + String(lv);
        this.lastLv.string = 'Lv' + String(lv + 1);
        this.weaponLv.string = "+" + lv + "的传承之剑";
        if (totalGold < needGold) {
            this.startBtn.getComponent(cc.Button).interactable = false;
            this.gold.node.color = cc.color(255, 45, 9);
        }
        else {
            this.gold.node.color = cc.color(255, 232, 142);
        }
        this.gold.string = String(needGold);
        this, this.lvProgressBar.progress = curLvState;

    }
    //刷新界面UI
    public static upDataWeaponLv(self, data) {
        var playerObj = LocalStorage.getObj("Player");
        var totalGold = playerObj["gold"];
        var lv = playerObj["oneselfWeapon"].lv;
        var curLvState = playerObj["oneselfWeapon"].curLvState;
        var lvUpData = LoadResData.lvupData;
        var needGold = 0;
        var num = data * 2 / 100;

        var n: number = Math.round((num + curLvState) * 100) / 100;
        var num = 0;
        var state = Math.floor(n);;
        if (n > 1) {
            num = Math.round((n - state) * 100) / 100;
        }
        else {
            num = n;
        }
        var curLv = Math.floor(lv + state);
        for (let i = 0; i < lvUpData.length; i++) {
            if (lv == lvUpData[i]["lv"]) {
                needGold = lvUpData[i]["price"];
            }
            if (curLv > lv) {
                self.upDataWeaponLv(curLv, lv);
            }
        }
        var gold = totalGold - needGold;
        var list = {
            "gold": gold,
            //自己的武器（等级，当前等级强化百分比状态，强化所需金币）
            oneselfWeapon: {
                lv: curLv,
                curLvState: num,
            },
        }
        LocalStorage.setObj("Player", list);
        self.showUI();
        Context.get().messenger.sendMessage(WxEvent.ON_CHANGEGOLD, [gold]);
        SubContextView.updateUserData("lv", { "level": curLv, "uuid": 0, "timestamp": 0, "wxgame": {} });
    }
    public upDataWeaponLv(curLv, lv) {

        var lvUpData = LoadResData.lvupData;
        var unlock_weapons = 0;
        var index: number = lv + 1;
        var data = null;
        var showWeapon: any = LocalStorage.getObj("weaponLv");
        for (let i = 0; i < lvUpData.length; i++) {
            if (index == Number(lvUpData[i]["lv"])) {
                unlock_weapons = lvUpData[i]["unlock_weapons"];
                data = { "id": unlock_weapons, "lv": 1 };
            }
        }
        showWeapon.push(data);
        LocalStorage.setObj("weaponLv", showWeapon);
    }
}